/*
 * InternalFramesDemo.java  0.4.0 / Oct 24, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.demos.swingset;

import com.cute4j.swing.AbstractAction;
import com.cute4j.swing.Button;
import com.cute4j.swing.CheckBox;
import com.cute4j.swing.Component;
import com.cute4j.swing.DesktopPane;
import com.cute4j.swing.ImageIcon;
import com.cute4j.swing.InternalFrame;
import com.cute4j.swing.Label;
import com.cute4j.swing.ScrollPane;
import com.cute4j.swing.TextField;
import com.cute4j.swing.events.ActionEvent;
import com.cute4j.swing.graphics.Color;
import com.cute4j.swing.layouts.formlayout.CellConstraints;
import com.cute4j.swing.layouts.formlayout.FormLayout;

/**
 * Demonstration of the InternalFrame class.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class InternalFramesDemo extends DemoModule {

// Public Constructor. ---------------------------------------------------------

  /**
   * Constructs new InternalFramesDemo object.
   */
  public InternalFramesDemo() {
    super("InternalFrame Demo", "Demonstration of the InternalFrame class.");
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected Component buildDemoComponent() {
    return new InternalFramesPane();
  }

// Inner Classes. --------------------------------------------------------------

  /**
   * InternalFrames Pane.
   */
  class InternalFramesPane extends DesktopPane {

    private int frameNumber;
    private CheckBox frameResizeable;
    private CheckBox frameCloseable;
    private CheckBox frameIconifiable;
    private CheckBox frameMaximizeable;
    private TextField frameTitle;

    public InternalFramesPane() {
      InternalFrame frameGenerator = add(buildFrameGenerator());
      frameGenerator.pack();
      frameGenerator.setLocation(375, 20);
      InternalFrame frame0 = add(createFrame("fish.png"));
      frame0.setBounds(15, 260, 320, 230);
      addFrame("fish.png");
      addFrame("sun.png");
      addFrame("cab.png");
      addFrame("moon.png");
      setSelectedComponent(frameGenerator);
    }

    // private methods

    private void addFrame(String imageName) {
      InternalFrame frame = createFrame(imageName);
      int frameBorder = InternalFrame.BORDER_TITLE;
      if (frameCloseable.isSelected()) {
        frameBorder = frameBorder | InternalFrame.BORDER_SYSTEM_MENU;
      }
      if (frameIconifiable.isSelected()) {
        frameBorder = frameBorder | InternalFrame.BORDER_MINIMIZE_BUTTON;
      }
      if (frameMaximizeable.isSelected()) {
        frameBorder = frameBorder | InternalFrame.BORDER_MAXIMIZE_BUTTON;
      }
      frame.setBorderDecoration(frameBorder);
      int framePosition = frameNumber - 1;
      frame.setBounds(20 * (framePosition % 10), 20 * (framePosition % 10), 225,
                      150);
      if (!frameResizeable.isSelected()) {
        frame.setFixedSize(frame.getSize());
      }
      add(frame);
    }

    private InternalFrame buildFrameGenerator() {
      frameResizeable = new CheckBox("Resizeable");
      frameResizeable.setSelected(true);
      frameCloseable = new CheckBox("Closeable");
      frameCloseable.setSelected(true);
      frameIconifiable = new CheckBox("Iconifiable");
      frameIconifiable.setSelected(true);
      frameMaximizeable = new CheckBox("Maximizeable");
      frameMaximizeable.setSelected(true);
      frameTitle = new TextField("Frame");
      FormLayout layout = new FormLayout(
                            "p:g, max(60dlu;p), 14dlu, 9dlu, max(72dlu;p), p:g",
                            "p, 9dlu, p, 9dlu, p, 4dlu, p, 8dlu, p");
      InternalFrame jqif = new InternalFrame(layout,
                                             "Internal Frame Generator");
      jqif.setBorderDecoration(InternalFrame.BORDER_TITLE);
      jqif.setInsets(10, 10, 10, 10);
      CellConstraints cc = new CellConstraints();
      jqif.add(new Button(new GeneratorAction("fish")), cc.xyw(2, 1, 2));
      jqif.add(new Button(new GeneratorAction("moon")), cc.xy(5, 1));
      jqif.add(new Button(new GeneratorAction("sun")), cc.xyw(2, 3, 2));
      jqif.add(new Button(new GeneratorAction("cab")), cc.xy(5, 3));
      jqif.add(frameResizeable, cc.xyw(2, 5, 2));
      jqif.add(frameCloseable, cc.xy(5, 5));
      jqif.add(frameIconifiable, cc.xyw(2, 7, 2));
      jqif.add(frameMaximizeable, cc.xy(5, 7));
      jqif.add(new Label("Frame title:"), cc.xy(2, 9));
      jqif.add(frameTitle, cc.xyw(3, 9, 3));
      return jqif;
    }

    private InternalFrame createFrame(String imageName) {
      String title = frameTitle.getText();
      if ("Frame".equals(title)) {
        title = "Frame " + frameNumber;
      }
      ++frameNumber;
      InternalFrame jqif = new InternalFrame(title);
      String imagePath = "resources/images/image_club/misc/" + imageName;
      ImageIcon image = new ImageIcon(loadResource(imagePath));
      Label imageLabel = new Label(image);
      imageLabel.setBackground(Color.WHITE);
      ScrollPane imageScrollPane = new ScrollPane(imageLabel);
      jqif.add(imageScrollPane);
      return jqif;
    }

    /**
     * Generator Action Class.
     */
    private class GeneratorAction extends AbstractAction {
      private final String imageName;
      GeneratorAction(String imageName) {
        this.imageName = imageName + ".png";
        String iconName = "resources/images/image_club/misc/" + imageName +
                          "_small.png";
        setSmallIcon(new ImageIcon(loadResource(iconName)));
      }
      public void actionPerformed(ActionEvent e) {
        addFrame(imageName);
      }
    }

  }

// Main Method. ----------------------------------------------------------------

  /**
   * Starts demo as standalone application.
   *
   * @param args the application arguments
   */
  public static void main(String[] args) {
    new InternalFramesDemo().show();
  }

}
